//
//  LoginViewController.m
//  DirectMember
//
//  Created by jzd on 2018/6/20.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "SJTabbarController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImgCons;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self adjustTextField];

    [self autoScrollViewContentSize];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.topImgCons.constant = 420 * SJScreenH / 667;
}
-(void)adjustTextField{
    UIImage *im = [UIImage imageNamed:@"userName"];
    UIImageView *iv = [[UIImageView alloc] initWithImage:im];
    UIView *lv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 35)];//宽度根据需求进行设置，高度必须大于 textField 的高度
    iv.center = lv.center;
    [lv addSubview:iv];
    
    //设置 textField 的左侧视图
    //设置左侧视图的显示模式
    self.userName.leftViewMode = UITextFieldViewModeAlways;
    self.userName.leftView = lv;
    
    
    UIImage *img = [UIImage imageNamed:@"pwd"];
    UIImageView *ivg = [[UIImageView alloc] initWithImage:img];
    UIView *lvg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 35)];//宽度根据需求进行设置，高度必须大于 textField 的高度
    ivg.center = lvg.center;
    [lvg addSubview:ivg];
    
    //设置 textField 的左侧视图
    //设置左侧视图的显示模式
    self.pwd.leftViewMode = UITextFieldViewModeAlways;
    self.pwd.leftView = lvg;
}
-(void)autoScrollViewContentSize{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardHidn:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyBoardShow:(NSNotification *)noti{
    //获取键盘的高度
    NSDictionary *userInfo = [noti userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    
    self.topCons.constant = -(height - 80);
    [UIView animateWithDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
        [self.view layoutIfNeeded];
    }];
    
}
-(void)keyBoardHidn:(NSNotification *)noti{
    NSDictionary *userInfo = [noti userInfo];
    
    self.topCons.constant = 80;
    [UIView animateWithDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
        [self.view layoutIfNeeded];
    }];
}
- (IBAction)login:(UIButton *)sender {
    SJTabbarController *tab = [[SJTabbarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tab;
}
- (IBAction)registerBut:(UIButton *)sender {
    RegisterViewController *vc = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)rememberPwd:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (IBAction)forgetPwd:(UIButton *)sender {
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
